var express = require('express')
var router = express.Router();
var { addressModel } = require('../model/address')

// 收货地址
router.get('/getAddress', async (req, res) => {
    let data = await addressModel.find().sort({status:1})
    console.log(data,'data');
    res.send(data)
})
// 置顶地址信息
router.get('/first', async (req, res) => {
    let { id, status } = req.query
    let data = await addressModel.find()
    data.map(i => {
        if (i.id == id) {
            i.status = status?0:1
            i.save()
        }
        else {
            i.status = status?1:0
            i.save()
        }
    })
    res.send({
        data,
        code:'200',
        msg:'置顶收货地址'
    })
})
// 删除收货地址
router.delete('/delAddress', async (req, res) => {
    let id = req.query.id
    await addressModel.deleteOne({_id:id})
    res.send({
        code:200,
        msg:'删除收货地址成功'
    })
})

// 修改收货地址
router.post('/editAddress', async (req, res) => {
    let id = req.query.id
    let body = req.body.body
    await addressModel.updateOne({_id:id},body)
    res.send({
        code:200,
        msg:'修改收货地址成功'
    })
})

module.exports = router;